package com.servlet;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Upload extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Upload() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		/* System.out.println("contextType:"+request.getContentType());
		 System.out.println("localAddr:"+request.getLocalAddr());
		 System.out.println("localName:"+request.getLocalName());
		 System.out.println("pathInfo:"+request.getPathInfo());
		 System.out.println("PathTranslated:"+request.getPathTranslated());
		 System.out.println("Protocol:"+request.getProtocol());
		 System.out.println("QueryString:"+request.getQueryString());
		 System.out.println("RemoteAddr:"+request.getRemoteAddr());
		 System.out.println("getRequestedSessionId:"+request.getRequestedSessionId());
		 System.out.println("getRequestURI:"+request.getRequestURI());
		 System.out.println("getScheme:"+request.getScheme());
		 System.out.println("getServerName:"+request.getServerName());
		 System.out.println("getRemoteUser:"+request.getRemoteUser());
		 System.out.println("getServletPath:"+request.getServletPath());
		 System.out.println("getLocale:"+request.getLocale());
		 System.out.println("getReader:"+request.getReader());
		 System.out.println("UserPrincipal:"+request.getUserPrincipal());*/
		 InputStream iStream=request.getInputStream();
		 System.out.println("name:"+request.getParameter("name"));
		 System.out.println("img:"+request.getParameter("img"));
		/*
		 * int i=iStream.read(); while(i!=-1){ // System.out.print(i);
		 * i=iStream.read(); } iStream.close();
		 */
		 System.out.println(request.getSession().getServletContext().getContextPath()+"\n"
				 +request.getSession().getServletContext().getRealPath("test.txt"));
		PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));
		ServletInputStream in = request.getInputStream();
		int i = in.read();
		while (i != -1) {
			pw.print((char) i);
			i = in.read();
		}
		pw.close();


	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
